public class MyQueue {
    static class ListNode {
        int val;
        ListNode pre;
        ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;
    //入队
    public void offer(int val) {
        ListNode newNode = new ListNode(val);
        if (head == null) {
            head = newNode;
            last = newNode;
        }else {
            newNode.pre = last;
            last.next = newNode;
            last = newNode;
        }
    }
    //出队
    public int poll() {
        if (head == null) {
            throw new ExceptionOfEmpty("队列为空，无法操作");
        }
        int val = -1;
        //当队列只有一个元素的时候要进行特殊处理，不然后面的程序会报空指针异常
        if (head.next == null) {
            val = head.val;
            head = null;
            last = null;
            return val;
        }
        //对于其他正常位置元素的处理
        val = head.val;
        head = head.next;
        head.pre = null;
        return val;
    }
    //拿出队列的首个元素进行查看
    public int peek() {
        if (head == null) {
            throw new ExceptionOfEmpty("队列为空，无法操作");
        }
        int val = head.val;
        return val;
    }
}